package com.cao.meet.swipelist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cao.meet.swipelist.view.MyLinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by small_world.
 * Date: 2018/9/13.
 * Time: 14:45.
 * purpose:
 */

public class MergeListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<DataHolder> mDataList = new ArrayList<DataHolder>();
    private View.OnClickListener mDelClickListener;
    private MyLinearLayout.OnScrollListener mScrollListener;

    public MergeListAdapter(Context context, List<DataHolder> dataList, View.OnClickListener delClickListener,MyLinearLayout.OnScrollListener listener) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        if (dataList != null && dataList.size() > 0) {
            mDataList.addAll(dataList);
        }
        mDelClickListener = delClickListener;
        mScrollListener = listener;
    }

    public void removeItem(int position) {
        mDataList.remove(position);
        notifyDataSetChanged();
    }

    public void addItem(DataHolder item) {
        mDataList.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = mInflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DataHolder item = mDataList.get(position);
        holder.title.setText(item.title);

        item.rootView = (MyLinearLayout) convertView.findViewById(R.id.lin_roots);
        item.rootView.scrollTo(0, 0);
        item.rootView.setOnScrollListener(mScrollListener);

        //点击删除实现方法一:直接处理
//        TextView delTv = (TextView) convertView.findViewById(R.id.del);
//        delTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mDataList.remove(position)
//            }
//        });

        //点击删除实现方法二:交由外部处理:
        TextView delTv = (TextView) convertView.findViewById(R.id.del);
        delTv.setOnClickListener(mDelClickListener);
        return convertView;
    }

    private static class ViewHolder {
        public TextView title;
    }

    public static class DataHolder {
        public String title;
        public MyLinearLayout rootView;
    }

}
